/*
 * bright_regim.c
 *
 *  Created on: 2024 Feb 20
 *      Author: simeonidi03
 */
#include "bright_regim.h"



void set_color(uint16_t* one_led, const int red, int green, const int blue){
	int bit_buf = 0;
    int buf = green;
    int number = green;
    int binaryArray[8];
    for (int i = 0; i < 8; ++i) {
        binaryArray[i] = (number >> (7 - i)) & 1;
    }
    for(int i = 0; i < 8; ++i) {
        one_led[i] = BIT_ON * binaryArray[i];
        if(!binaryArray[i]) {
            one_led[i] = BIT_OFF;
        }
    }
    number = red;
    for (int i = 0; i < 8; ++i) {
        binaryArray[i] = (number >> (7 - i)) & 1;
    }
    int i_obr = 8;
    for(int i = 0; i < 8; ++i) {
        one_led[i_obr] = BIT_ON * binaryArray[i];
        if(!binaryArray[i]) {
            one_led[i_obr] = BIT_OFF;
        }
        i_obr++;
    }
     number = blue;
     for (int i = 0; i < 8; ++i) {
         binaryArray[i] = (number >> (7 - i)) & 1;
     }
    i_obr = 16;
    for(int i = 0; i < 8; ++i) {
        one_led[i_obr] = BIT_ON * binaryArray[i];
        if(!binaryArray[i]) {
            one_led[i_obr] = BIT_OFF;
        }
        i_obr++;
    }
}


void set_led_number(uint16_t* arr, int first_led, int last_led){
    int arr_index = 24;
    while(arr_index < BIT_NUMBER - 48){
        for(int i = 0; i < 24; ++i){
            arr[arr_index] = arr[i];
            arr_index++;
        }
    }
    while (arr_index < BIT_NUMBER) {
        arr[arr_index] = 100;
        arr_index++;
    }
}


void led_blink( uint16_t* src_buffer, work_param* first)
{

    static int blinkCounter = 0;
	if (blinkCounter % 1000 < 333) {
		set_color(src_buffer, first->red1, first->green1, first->blue1);
		set_led_number(src_buffer, 0, 0);
	} else if (blinkCounter % 1000 > 333 && blinkCounter % 1000 < 666) {
		set_color(src_buffer, first->red2, first->green2, first->blue2);
		set_led_number(src_buffer, 0, 0);
	} else {
		set_color(src_buffer, first->red3, first->green3, first->blue3);
		set_led_number(src_buffer, 0, 0);
	}

	blinkCounter++;

	if (blinkCounter >= 1000) {
		blinkCounter = 0;
	}
}



void turn_off_led(uint16_t* arr){
	for(int i = 0; i < BIT_NUMBER - 48; i++){
		arr[i] = 68;
	}

	for(int i = BIT_NUMBER - 48; i < BIT_NUMBER; i++){
		arr[i] = 100;
	}
}

void set_single_led_color(uint16_t* arr, int led_number, const int red, int green, const int blue, const int bright) {

	int color[3] = { red * bright / 255, green * bright / 255, blue * bright / 255 };
	if (led_number >= 1 && led_number <= LED_NUMBER) {
        uint16_t* one_led = arr + (led_number - 1) * 24;
        set_color(one_led, color[0], color[1], color[2]);
    }

}


void led_wave( uint16_t* src_buffer, const int red, int green, const int blue){

    static int currentLED = 0;  // Variable to keep track of the current LED
    unsigned char color[3] = {red, green, blue};  // Initial color (Red)
    static int numLEDs = 6;  // Number of LEDs to light up consequently

    // Turn off the previously lit LEDs
    for (int i = 0; i < numLEDs; i++) {
    	set_single_led_color(src_buffer,(currentLED + i) % 14, 0, 0, 0, 0);
    }

    // Move to the next starting LED
    currentLED = (currentLED + 1) % 14;

	// Turn on the next set of LEDs with the specified color
	for (int i = 0; i < numLEDs; i++) {
		set_single_led_color(src_buffer, (currentLED + i) % 14, color[0], color[1], color[2], i * i * i * (255 / (numLEDs * numLEDs * numLEDs )));
	}



}




